package com.gitee.shigen.security.controller;

import cn.hutool.core.collection.CollUtil;
import com.gitee.shigen.common.msg.Result;
import com.gitee.shigen.security.common.StatusEnum;
import com.gitee.shigen.security.request.PageRequest;
import com.gitee.shigen.security.service.MonitorService;
import com.gitee.shigen.security.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author shigenfu
 * @date 2024/5/25 17:42
 */
@Slf4j
@RestController
@RequestMapping("/api/monitor")
public class MonitorController {

    @Resource
    private MonitorService monitorService;

    /**
     * 在线用户列表
     *
     * @param pageRequest 分页参数
     */
    @GetMapping("/online/user")
    public Result<List<String>> onlineUser(@Valid PageRequest pageRequest) {
        List<String> pageResult = monitorService.onlineUser(pageRequest);
        return Result.success(pageResult);
    }

    /**
     * 批量踢出在线用户
     *
     * @param names 用户名列表
     */
    @DeleteMapping("/online/user/kickOut")
    public Result<String> kickOutOnlineUser(@RequestBody List<String> names) {
        if (CollUtil.isEmpty(names)) {
            throw new SecurityException(StatusEnum.PARAM_NOT_NULL.getMessage());
        }
        if (names.contains(SecurityUtil.getCurrentUsername())) {
            throw new SecurityException(StatusEnum.KICKOUT_SELF.getMessage());
        }
        monitorService.kickOut(names);
        return Result.success("");
    }
}
